#include "my_header.h"

int main(int argc, char *argv[]){                                  
    //预期输入: 
    //./client(项目目录) 192.168.174.128(本机ip) 12345(端口)

    ARGS_CHECK(argc, 3);// 参数数量判错
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);// AF_INET 代表ipv4
                                                 // SOCK_STREAM 代表有序、可靠的数据传输流
    struct sockaddr_in addr;// 给connect做准备，初始化addr结构体
    addr.sin_family = AF_INET;// ipv4
    addr.sin_port = htons(atoi(argv[2]));// 端口(字符 -> 整型)，小端 -> 大端
    addr.sin_addr.s_addr = inet_addr(argv[1]);// ip地址
    int ret = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));// sockaddr_in结构体强转sockaddr
    ERROR_CHECK(ret, -1, "connect");// 判错
	
    ret = authPasswd(sockfd); //密码验证
    if (ret != 0) {           //验证成功返回0，失败返回-1
        close(sockfd);
        return 1;
    }

    // connect success.
    printf("connection success! please input your request. press 'h' for help. 'q' for quit.\n");
    char buf[1024];// 承接输入目录
    char filename[1024];// 承接文件名
    while(1){
        bzero(buf, 1024);
        bzero(filename, 1024);
        scanf(" %[^\n]", buf);// 忽略实际内容前的空格，一直读取到换行符为止
        int size_requset = 0;// 承接指令长度
        for(int i = 0; i < 1024; i ++){// 获取指令长度
            if(buf[i] != '\0'){
                size_requset ++;
            }
            else{
                break;
            }
        }
        if(size_requset == 1 && strcmp(buf, "h") == 0){
            printf("user is seeking for help.\n");
            continue;
        }
        if(size_requset == 1 && strcmp(buf, "q") == 0){
            printf("user is seeking for quit.\n");
            break;
        }
        // 用空格切割字符串并对空格前的字符串进行判断，转到不同的函数
        char *token = strtok(buf, " ");// 承接切割出的字符串，第一次切割到空格前
        train_t train;
        train.type = 0;
        train.lenth = 0;
        // 分析空格后的请求
        if(strcmp(token, "cd") == 0){// 切换目录操作
            printf("user put an cd event.\n");
            token = strtok(NULL, " ");
            strcpy(train.value, token);
            sends(sockfd, &train, CD, NULL);
            continue;
        }
        if(strcmp(token, "ls") == 0){// 查看当级目录操作
            printf("user put an ls event.\n");
            sends(sockfd, &train, LS, NULL);
            continue;
        }
        if(strcmp(token, "pwd") == 0){// 查看路径操作
            printf("user put an pwd event.\n");
            sends(sockfd, &train, PWD, NULL);
            continue;
        }
        if(strcmp(token, "mkdir") == 0){// 创建目录操作
            printf("user put an mkdir event.\n");
            strcpy(filename, strtok(NULL, " "));
            sends(sockfd, &train, MKDIR, filename);
            continue;
        }
        if(strcmp(token, "puts") == 0){// 上传文件操作
            printf("user put an put file event.\n");
            strcpy(filename, strtok(NULL, " "));
            sends(sockfd, &train, PUTS, filename);
            continue;
        }
        if(strcmp(token, "gets") == 0){// 下载文件操作
            printf("user put an get file event.\n");
            strcpy(filename, strtok(NULL, " "));
            sends(sockfd, &train, GETS, filename);
            continue;
        }
        if(strcmp(token, "remove") == 0){// 删除文件操作
            printf("user put an remove event.\n");
            strcpy(filename, strtok(NULL, " "));
            sends(sockfd, &train, REMOVE, filename);
            continue;
        }
        else{
            printf("wrong input, please input correctly.\n");
            continue;
        }
    }
    return 0;
}
